package com.iwaster.servlet;

import com.iwaster.utils.UploadUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

/*
 *${PACKAGE_NAME}
 *@author Ouka
 *9:17
 */
@WebServlet(name = "DownloadServlet",urlPatterns ="/download")
public class DownloadServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String filename = request.getParameter("filename");
        String shortfilename = filename.split("_")[1];
        String path = this.getServletContext().getRealPath("/WEB-INF/upload");
        String newPath = UploadUtils.createNewPath(path, shortfilename);
        FileInputStream fis=new FileInputStream(newPath+"/"+filename);
        byte[] buf=new byte[1024*4];
        int len=0;
        while((len=fis.read(buf))!=-1){
            response.getOutputStream().write(buf,0,len);
        }
        //4关闭
        fis.close();

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
